#include "CWatchDog.h"
#include <qmessagebox.h>


#define CONTROLLED_APP qApp->applicationDirPath()+"/testdemo.exe"


#pragma execution_character_set("utf-8")
CWatchDog::CWatchDog(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    OnInitDialog();
}

CWatchDog::~CWatchDog()
{}


void CWatchDog::OnReStart(int exitCode, QProcess::ExitStatus exitStatus)
{
    if (0 != exitCode)
    {
        if(QMessageBox::Yes ==  QMessageBox::question(this, tr("question"), tr("Whether to restart the application."),QMessageBox::Yes | QMessageBox::No))
            m_watch_dog.start(CONTROLLED_APP);
    }
        
}

void CWatchDog::OnInitDialog()
{
    connect(&m_watch_dog, &QProcess::started, [] {});
    connect(&m_watch_dog, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &CWatchDog::OnReStart);
    connect(&m_watch_dog, &QProcess::errorOccurred, [this](QProcess::ProcessError error) {
        QMessageBox::information(this, tr("information"), m_watch_dog.errorString()); });
    m_watch_dog.start(CONTROLLED_APP);
}


